<?php

class ActiveRecordLogView extends Portlet
{
    // {{{ Members
    public $title;
    public $cssClass='portlet';
    public $header1CssClass='portlet-decoration';
    public $header2CssClass='portlet-title';
    public $contentCssClass='portlet-content';
    public $visible=true;
    public $model;
    public $viewId;
    private $_activeRecordLogs;
    // }}} 
    // {{{ init
    public function init()
    {
        $this->viewId = uniqid('arlogview_');
        $this->title = Yii::t('site','Record Log');
        // Check if model has ActiveRecordLogBehavior?
        $behaviors = $this->model->behaviors();
        if(!array_key_exists('ActiveRecordLogBehavior',$behaviors))
            $this->visible = false;

        if(!$this->visible)
            return;

        // get ActiveRecord log entries
        $model = new ActiveRecordLog;
        $attributes = array(
            'modelName' => get_class($this->model),
            'recordId' => $this->model->getPrimaryKey(),
        );
        $condition = array(
            'limit' => 10,
            'order' => 'timestamp DESC'
        );
        $this->_activeRecordLogs = $model->findAllByAttributes($attributes,$condition);
         

        echo "<div id=\"{$this->viewId}\" style=\"cursor:pointer\" class=\"{$this->cssClass}\" title=\"".Yii::t('site','Toggle Log Items')."\">\n";
        echo "\t<div class=\"{$this->header1CssClass}\">\n";
        if($this->title!==null)
            echo "\t\t<div class=\"{$this->header2CssClass}\">{$this->title}</div>\n";
        echo "\t</div>\n";
        echo "\t<div class=\"{$this->contentCssClass}\">\n";
    } // }}} 
    // {{{ run
    public function run()
    {
        if(!$this->visible)
            return;

        $cs = Yii::app()->clientScript;
        $cs->registerScript($this->viewId,"jQuery('#{$this->viewId}').click(function(){jQuery('#{$this->viewId}_table').toggle();});");
        
        $this->renderContent();
        echo "\t</div><!-- {$this->contentCssClass} -->\n";
        echo "</div><!-- {$this->cssClass} -->";
    } // }}} 
    // {{{ renderContent
    protected function renderContent()
    {
        $this->render(
	    'activeRecordLog',
            array(
                'viewId' => $this->viewId,
		'activeRecordLogs' => $this->_activeRecordLogs
	    )
	);
    } // }}} 
}
/* vim:set ai sw=4 sts=4 et fdm=marker fdc=4: */ 
?>
